package cn.z2huo.knowledge.concurrency.synchronizedkeyword.reentrant;

import java.util.concurrent.TimeUnit;

/**
 * <p>递归调用测试
 *
 * @author z2huo
 */
class RecursiveTest {

    public synchronized void method(int n) {
        if (n <= 0) {
            return;
        }
        System.out.println(Thread.currentThread().threadId() + ", n = " + n);
        method(n - 1);
    }

    public static void main(String[] args) throws InterruptedException {

        int count = 10;

        RecursiveTest recursiveTest = new RecursiveTest();

        Runnable runnable = () -> {
            recursiveTest.method(count);
        };

        Thread thread = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        thread2.start();
    }

}
